using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using VistaDB.Provider;

using NSimpleDB.Service.VistaDb;

namespace test.Service.VistaDb
{
    class DBHelpers
    {
        public static DataTable GetDomainDirectory(string dbFilename, bool includeDeleted)
        {
            using (VistaDBConnection conn = new VistaDBConnection("data source=" + dbFilename))
            {
                VistaDBDataAdapter adap = new VistaDBDataAdapter("select * from DomainDirectory" + (includeDeleted ? "" : " where not scheduledForDeletion"), conn);
                DataTable tb = new DataTable("DomainDirectory");
                adap.Fill(tb);
                return tb;
            }
        }

        public static DataTable GetDomainByIdDomain(string dbFilename, int idDomain)
        {
            using (VistaDBConnection conn = new VistaDBConnection("data source=" + dbFilename))
            {
                VistaDBDataAdapter adapD = new VistaDBDataAdapter("select * from Domain" + idDomain, conn);
                DataTable tbD = new DataTable("Domain");
                adapD.Fill(tbD);
                return tbD;
            }
        }


        public static DataTable GetItem(string dbFilename, string domainName, string itemName)
        {
            DataTable tbDD = DBHelpers.GetDomainDirectory(dbFilename, false);
            int idDomain = (int)tbDD.Select(string.Format("domainName='{0}'", domainName))[0]["idDomain"];

            using (VistaDBConnection conn = new VistaDBConnection("data source=" + dbFilename))
            {
                VistaDBDataAdapter adapD = new VistaDBDataAdapter(
                    string.Format("select * from Domain{0} where itemName='{1}'", idDomain, itemName), 
                    conn);
                DataTable tbI = new DataTable("Item");
                adapD.Fill(tbI);
                return tbI;
            }
        }
    }
}
